package com.grablist.feihu.grablist.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.comm.MyType;
import com.grablist.feihu.grablist.comm.RetrofitHelp;
import com.grablist.feihu.grablist.event.CloseActEvent;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.ui.actviity.login.LoginAct;
import com.grablist.feihu.grablist.ui.widget.BAlertDialog;
import rx.functions.Action1;

/**
 * Created by admin on 2017/12/2.
 */

public class SettingAct extends BaseActivity {

    @Bind(R.id.activity_setteing)
    TextView setteing;
    @Bind(R.id.activity_login_out)
    TextView loginOut;

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_setteing);

    }

    @Override
    protected void setup() {
        setStateText();
        setToolBar("设置", "");
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        RxView.clicks(setteing)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startAct(ForgetPwdAct.class, null);
                    }
                });

        RxView.clicks(loginOut)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showInfoDialog("是否退出登录");
                    }
                });

    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.Logout) {
            finishAct();
        }
    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
    /**
     * 按钮提示 确定 取消
     *
     * @param info
     */
    public void showInfoDialog(String info) {
        final BAlertDialog bAlertDialog = new BAlertDialog(this);
        bAlertDialog.setTitle("提示").setMessage(info)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApplication.userInfo=null;
                        RetrofitHelp.logout();
                        startAct(LoginAct.class,null);
                        EventBus.getDefault().post(new CloseActEvent(MyType.Logout));
                        finishAct();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

            }
        }).show();

    }
}

